package com.example.zookeeper;

import com.example.zookeeper.config.NettyServer;
import com.example.zookeeper.register.ServiceRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author jw
 */
@SpringBootApplication
@EnableConfigurationProperties
@PropertySource(value = "classpath:netty.properties", encoding = "utf-8")
public class ZookeeperApplication implements CommandLineRunner {

    @Value("${netty.server.port}")
    private int port;

    @Autowired
    private ServiceRegistry serviceRegistry;

    public static void main(String[] args) {
        SpringApplication.run(ZookeeperApplication.class, args);
    }

    @Override
    public void run(String... args) {
        try {
            //服务注册
            String ip = InetAddress.getLocalHost().getHostAddress();
            serviceRegistry.register(ip+":"+port);

            NettyServer nettyServer = new NettyServer();
            nettyServer.start(port);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
